<?php

namespace App\Jobs\Attachment;
// 加载采集环境

use App\Models\WxAttachment;
use App\Wen\Utils\ApiUtils;
use App\Wen\Utils\Settings;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Qcloud\Cos\Client;

class VideoCosReviewQuery implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $cosClient = null;
    private $Bucket = null;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    private function initConfig(){
        if($this->cosClient === null){
            $cosConfig = Settings::get('cosv5', []);
            $this->cosClient = new Client(array(
                'region' => $cosConfig['region'],
                'scheme' => 'https',
                'credentials' => array(
                    'secretId' => $cosConfig['secretId'],
                    'secretKey' => $cosConfig['secretKey'],
                )
            ));
            $this->Bucket = $cosConfig['bucket'] . '-' . $cosConfig['appId'];
        }
    }


    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->initConfig();
        $attachment = WxAttachment::where('cos_review', 5)->whereIn('type', ['mp4'])->first();
        if($attachment){
            ApiUtils::cos_review_query($this->cosClient, $this->Bucket, $attachment);
        }
    }
}
